package com.bqtv.sport.ui.main.fragment

import android.content.Intent
import android.os.Bundle
import androidx.viewbinding.ViewBinding
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.app.AppFragment
import com.bqtv.sport.databinding.FragmentContactBinding
import com.bqtv.sport.ui.im.FriendHomeActivity
import com.bqtv.sport.ui.im.MyGroupListActivity
import com.bqtv.sport.ui.im.MyNewFriendActivity
import com.bqtv.sport.ui.im.SearchFriendActivity
import com.bqtv.sport.ui.mine.QRCodeScanActivity
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuicontact.TUIContactService
import com.tencent.qcloud.tuikit.tuicontact.bean.ContactItemBean
import com.tencent.qcloud.tuikit.tuicontact.classicui.pages.BlackListActivity
import com.tencent.qcloud.tuikit.tuicontact.classicui.pages.GroupListActivity
import com.tencent.qcloud.tuikit.tuicontact.classicui.pages.NewFriendActivity
import com.tencent.qcloud.tuikit.tuicontact.classicui.pages.TUIContactFragment
import com.tencent.qcloud.tuikit.tuicontact.classicui.util.ClassicUIUtils
import com.tencent.qcloud.tuikit.tuicontact.classicui.widget.ContactListView
import com.tencent.qcloud.tuikit.tuicontact.presenter.ContactPresenter
import com.tencent.qcloud.tuikit.tuicontact.util.TUIContactLog


class ContactFragment : AppFragment<FragmentContactBinding, AppActivity<ViewBinding>>() {

    private var presenter: ContactPresenter? = null

    companion object {
        private val TAG: String = TUIContactFragment::class.java.getSimpleName()
        private const val INTENT_KEY_TYPE = "type"
        fun newInstance(): ContactFragment {
            val fragment = ContactFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

        fun newInstance(type: String?): ContactFragment {
            val fragment = ContactFragment()
            val bundle = Bundle()
            bundle.putString(INTENT_KEY_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getViewBinding() = FragmentContactBinding.inflate(layoutInflater)

    override fun initView() {

        presenter = ContactPresenter()
        presenter!!.setFriendListListener()
        binding.contactLayout.setPresenter(presenter)
        binding.contactLayout.initDefault()

        binding.contactLayout.getContactListView()
            .setOnItemClickListener(object : ContactListView.OnItemClickListener {
                override fun onItemClick(position: Int, contact: ContactItemBean) {
                    if (position == 0) {
                        val intent =
                            Intent(TUIContactService.getAppContext(), MyNewFriendActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        TUIContactService.getAppContext().startActivity(intent)
                    } else if (position == 1) {
                        val intent =
//                            Intent(TUIContactService.getAppContext(), GroupListActivity::class.java)
                            Intent(TUIContactService.getAppContext(), MyGroupListActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        TUIContactService.getAppContext().startActivity(intent)
                    } else {
                        if (contact.isTop() && contact.getExtensionListener() != null) {
                            val param: MutableMap<String?, Any?> = HashMap<String?, Any?>()
                            param.put(TUIConstants.TUIContact.CONTEXT, getActivity())
                            contact.getExtensionListener().onClicked(param)
                        } else {
//                            ClassicUIUtils.showContactDetails(contact.getId())
                            FriendHomeActivity.start(getAttachActivity()!!, contact.getId())
                        }
                    }
                }
            })
    }

    override fun initData() {
        binding.ivScan.setOnClickListener {
            QRCodeScanActivity.start(getAttachActivity()!!,object : QRCodeScanActivity.OnScanListener{
                override fun onSucceed(content: String?) {

                }

            })
        }
        binding.rlSearch.setOnClickListener {
            SearchFriendActivity.start( getAttachActivity()!!)
        }
    }
    fun reloadData() {
        if (binding.contactLayout != null) {
            binding.contactLayout.reloadData()
        }
    }
    override fun onResume() {
        super.onResume()
        TUIContactLog.i(TAG, "onResume")
    }
}

